package protocol;

import java.util.ArrayList;
import java.util.List;


import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import base.BaseProtocol;
import bean.CategoryInfoBean;
/**
 * 分类的网络连接
 * @author Anson-pc
 *
 */
public class CategoryProtocol extends BaseProtocol<List<CategoryInfoBean>> {

	@Override
	protected List<CategoryInfoBean> parseJsonString(String jsonString) {
		List<CategoryInfoBean> result = new ArrayList<CategoryInfoBean>();
		
		/*-----------  用安卓自带的json解析 ----------*/
		if (jsonString == null) {
System.out.println("数据为空");	
return null;
		}else {
			try {
				JSONArray  rootJsonArray = new JSONArray(jsonString);
				//解析标题头
				for (int i = 0; i < rootJsonArray.length(); i++) {
					
					JSONObject 	itemJsonObject = rootJsonArray.getJSONObject(i);
					
					String title = itemJsonObject.getString("title");
					
					//标题的Bean
					CategoryInfoBean titleBean = new CategoryInfoBean();
					titleBean.title = title;
					titleBean.isTitle = true;
					
					result.add(titleBean);
					
					//解析内容
					JSONArray infoArray = itemJsonObject.getJSONArray("infos");
					for (int j = 0; j < infoArray.length(); j++) {
						JSONObject infoJsonObject = infoArray.getJSONObject(j);
						String name1 = infoJsonObject.getString("name1");
						String name2 = infoJsonObject.getString("name2");
						String name3 = infoJsonObject.getString("name3");
						String url1 = infoJsonObject.getString("url1");
						String url2 = infoJsonObject.getString("url2");
						String url3 = infoJsonObject.getString("url3");
						
						CategoryInfoBean infoCategoryInfoBean = new CategoryInfoBean();
						infoCategoryInfoBean.name1 = name1;
						infoCategoryInfoBean.name2 = name2;
						infoCategoryInfoBean.name3 = name3;
						
						infoCategoryInfoBean.url1 = url1;
						infoCategoryInfoBean.url2 = url2;
						infoCategoryInfoBean.url3 = url3;
						
						result.add(infoCategoryInfoBean);
					}
					
				}
			} catch (JSONException e) {
				e.printStackTrace();
			}
			
			return result;
		}
		
	}

	@Override
	protected String getInterceKey() {
		return "category";
	}

}
